import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);
try {
  var defaultCity = localStorage.city || '北京'
} catch (e) { }

export default new Vuex.Store({
  state: {
    city: defaultCity,
    homeScroll: 0,
    bannerBack: true
  },
  actions: {
    changeCity(context, city) {
      context.commit("changeCity", city)
    },
    changeHomeScroll(context, scroll) {
      context.commit('changeHomeScroll', homeScroll);
    },
    showBannerBack() {
      context.commit('showBannerBack', bannerBack);
    }
  },
  mutations: {
    changeCity(state, city) {
      state.city = city
      try {
        localStorage.city = city;
      } catch (e) { }

    },
    changeHomeScroll(state, homeScroll) {
      state.homeScroll = homeScroll;
    },
    showBannerBack(state, bannerBack) {
      state.bannerBack = bannerBack;
    }
  },
  getters: {
    city(state) {
      let city = state.city;
      if (city.length > 2) {
        city = city.substring(0, 2).concat("...");
      }
      return city;
    }
  }

})